Imports System.Reflection
Imports Softelvdm.Controls
Imports Softelvdm.SftTreeNET

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' This sample demonstrates cell editing using edit controls and combo boxes.
    ' To prepare for this sample, create a new project with a blank form and add
    ' a SftTree/NET control named sftTree1.
    ' In addition, add a textbox named textbox1 and a combo box named comboBox1.
    sftTree1.Initializing = True
    sftTree1.Columns.Count = 3
    For i As Integer = 0 To 99
        Dim item As ItemClass
        item = sftTree1.ItemCollection.Add(New String() {"Can't", "Text", "Combo"})
    Next
    sftTree1.Columns.MakeOptimal(0, False)
    sftTree1.RecalcHorizontalExtent()
    sftTree1.Initializing = False
End Sub

Private Sub sftTree1_EditAllowed(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.EditAllowedEventArgs) Handles sftTree1.EditAllowed
    Debug.Write("** EditAllowed")
    DumpValues(e)
    If e.Cell.ColumnIndex = 0 Then ' Can't edit the first column
        e.Allowed = False
    End If
End Sub

Private Sub sftTree1_EditSetup(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.EditSetupEventArgs) Handles sftTree1.EditSetup
    Debug.Write("** EditSetup")
    DumpValues(e)

    If e.Cell.ColumnIndex = 1 Then
        ' We use a textbox for column 1 (remember, column 0 is not editable,
        ' because of the EditAllowed event
        ' set all textbox properties
        textBox1.Parent = sftTree1     ' make the tree control the parent window

        textBox1.Text = e.Cell.Text
        Dim h As Integer = textBox1.Height

        Dim r As Rectangle = e.rCell
        ' limit the control to the right pane - all cell editing takes place in right pane
        r.Intersect(SftTree1.Splitter.Right.ItemsArea)
        Dim vOffs As Integer = (r.Size.Height - TextBox1.Height) / 2
        TextBox1.Location = New Point(r.Location.X, r.Location.Y + vOffs)
        TextBox1.Width = r.Width

        ' make it visible and enable it
        textBox1.Enabled = True
        textBox1.Visible = True

        ' off we go!
        textBox1.BringToFront()
        textBox1.Focus()
        e.EditControl = textBox1

    ElseIf e.Cell.ColumnIndex = 2 Then

        ' We use a combobox for column 2
        comboBox1.Parent = sftTree1

        comboBox1.Items.Clear()
        comboBox1.Items.AddRange(New String() {"Selection 1", "Selection 2", "Selection 3", "Selection 4", "Selection 5"})

        comboBox1.Text = e.Cell.Text
        Dim h As Integer = comboBox1.Height

        Dim r As Rectangle = e.rCell
        ' limit the control to the right pane - all cell editing takes place in right pane
        r.Intersect(SftTree1.Splitter.Right.ItemsArea)
        Dim vOffs As Integer = (r.Size.Height - comboBox1.Height) / 2
        comboBox1.Location = New Point(r.Location.X, r.Location.Y + vOffs)
        comboBox1.Width = r.Width

        ' make it visible and enable it
        comboBox1.Enabled = True
        comboBox1.Visible = True

        ' off we go!
        comboBox1.BringToFront()
        comboBox1.Focus()
        e.EditControl = comboBox1
    End If
End Sub

Private Sub sftTree1_EditValidate(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.EditValidateEventArgs) Handles sftTree1.EditValidate
    If TypeOf e.EditControl Is TextBox Then
        If textBox1.Text = "" Then ' you MUST enter something
            e.Valid = False
            MessageBox.Show("Cell text can't be empty in this example.")
        End If
    ElseIf TypeOf e.EditControl Is ComboBox Then
        If comboBox1.Text = "" Then ' you MUST select something
            e.Valid = False
            MessageBox.Show("Cell text can't be empty in this example.")
        End If
    End If
End Sub

Private Sub sftTree1_EditEnded(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.EditEndedEventArgs) Handles sftTree1.EditEnded
    If TypeOf e.EditControl Is TextBox Then
        ' save the new cell text if necessary
        If (e.SaveValue) Then
            e.Cell.Text = textBox1.Text
        End If
        ' hide the text box and make the form the control's parent
        textBox1.Visible = False
        textBox1.Enabled = False
        textBox1.Parent = sftTree1.Parent
    ElseIf TypeOf e.EditControl Is ComboBox Then
        ' save the new cell text if necessary
        If (e.SaveValue) Then
            e.Cell.Text = comboBox1.Text
        End If
        ' hide the text box and make the form the control's parent
        comboBox1.Visible = False
        comboBox1.Enabled = False
        comboBox1.Parent = sftTree1.Parent
    End If
    Debug.Write("** EditEnded")
    DumpValues(e)
End Sub

Private Sub sftTree1_ItemClick(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.ItemClickEventArgs) Handles sftTree1.ItemClick
    Debug.Write("** ItemClick")
    DumpValues(e)
    If e.Area = ItemClickAreaEnum.CellSel Then
        e.Cell.Edit()  ' Start cell editing
    End If
End Sub

Private Sub textBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles textBox1.KeyDown
    If e.KeyCode = Keys.Up Then
        sftTree1.EditNavigate(EditNavigateEnum.Up)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Down Then
        sftTree1.EditNavigate(EditNavigateEnum.Down)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Left And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+Left
        sftTree1.EditNavigate(EditNavigateEnum.Left)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Right And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+Right
        sftTree1.EditNavigate(EditNavigateEnum.Right)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Home And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+Home
        sftTree1.EditNavigate(EditNavigateEnum.Home)
        e.Handled = True
    ElseIf e.KeyCode = Keys.End And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+End
        sftTree1.EditNavigate(EditNavigateEnum.End)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Tab And (e.Modifiers And Keys.Shift) <> 0 Then ' Shift+Tab
        sftTree1.EditNavigate(EditNavigateEnum.Left)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Tab And (e.Modifiers And Keys.Shift) = 0 Then ' Tab
        sftTree1.EditNavigate(EditNavigateEnum.Right)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Escape Then ' ESC
        sftTree1.EndEdit(False)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Return Then ' Return
        sftTree1.EndEdit(True)
        e.Handled = True
    End If
End Sub

Private Sub textBox1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles textBox1.PreviewKeyDown
    If (e.KeyCode = Keys.Tab) Then
        e.IsInputKey = True
    ElseIf (e.KeyCode = Keys.Escape) Then
        e.IsInputKey = True
    End If
End Sub

Private Sub comboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles comboBox1.KeyDown
    If (e.KeyCode = Keys.Up And (e.Modifiers And Keys.Control) <> 0) Then ' Ctrl+Up
        sftTree1.EditNavigate(EditNavigateEnum.Up)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Down And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+Down
        sftTree1.EditNavigate(EditNavigateEnum.Down)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Left And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+Left
        sftTree1.EditNavigate(EditNavigateEnum.Left)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Right And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+Right
        sftTree1.EditNavigate(EditNavigateEnum.Right)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Home And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+Home
        sftTree1.EditNavigate(EditNavigateEnum.Home)
        e.Handled = True
    ElseIf e.KeyCode = Keys.End And (e.Modifiers And Keys.Control) <> 0 Then ' Ctrl+End
        sftTree1.EditNavigate(EditNavigateEnum.End)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Tab And (e.Modifiers And Keys.Shift) <> 0 Then ' Shift+Tab
        sftTree1.EditNavigate(EditNavigateEnum.Left)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Tab And (e.Modifiers And Keys.Shift) = 0 Then ' Tab
        sftTree1.EditNavigate(EditNavigateEnum.Right)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Escape And Not comboBox1.DroppedDown Then ' ESC (only if dropdown is not showing)
        sftTree1.EndEdit(False)
        e.Handled = True
    ElseIf e.KeyCode = Keys.Return And Not comboBox1.DroppedDown Then ' Return (only if dropdown is not showing)
        sftTree1.EndEdit(True)
        e.Handled = True
    End If
End Sub

Private Sub comboBox1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles comboBox1.PreviewKeyDown
    If (e.KeyCode = Keys.Tab) Then
        e.IsInputKey = True
    ElseIf (e.KeyCode = Keys.Escape And Not comboBox1.DroppedDown) Then ' (only if dropdown is not showing)
        e.IsInputKey = True
    End If
End Sub

' This is a small helper routine to show all properties and fields of an object
Private Sub DumpValues(ByVal o As Object)
    Dim api() As PropertyInfo = o.GetType().GetProperties()
    For Each pi As PropertyInfo In api
        Debug.Write(" " & pi.Name & " " & pi.GetValue(o, New Object() {}))
    Next
    Dim afi() As FieldInfo = o.GetType().GetFields()
    For Each fi As FieldInfo In afi
        Dim t As Object = fi.GetValue(o)
        Dim s As String = "(null)"
        If Not t Is Nothing Then s = t.ToString()
        Debug.Write(" " & fi.Name & " " & s)
    Next
    Debug.WriteLine("")
End Sub

End Class